package com.example.syyao.myapp.Personal;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.syyao.myapp.Adapter.NVAdapter;
import com.example.syyao.myapp.CircleImageView;
import com.example.syyao.myapp.MainActivity;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.Response.Get_response_List;
import com.example.syyao.myapp.Response.Image;
import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class Personal_visitor extends SwipeBackActivity{
    private Get_response personal_respinse;
    private Button btn_choose;
    private ListView listView;
    private Get_response_List Movement_response;
    private List<List<Image>> imagesList;
    List<Map<String, Object>> myinformation;
    private String id;
    private NVAdapter nvAdapter;
    String status;
    int flag=1;//设置是否关注状态
    //粉丝
    private TextView fans;
    //关注的人
    private TextView conner;
    //昵称
    private  TextView nickname;
    //等级
    private TextView level;
    //头像
    private CircleImageView head_picture;
    //称号
    private TextView personal_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //状态栏半透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_visitor);
        overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);

        //获取另一个顶部布局文件
        LayoutInflater factory = LayoutInflater.from(Personal_visitor.this);
        View top=factory.inflate(R.layout.personal_visitor_top,null);


        listView= (ListView) findViewById(R.id.lv_vistor);

        btn_choose=(Button)top.findViewById(R.id.button);

        //假设已经关注
        btn_choose.setFocusable(true);
        Intent intent = getIntent();
        status = intent.getStringExtra("status").toString();
        id = intent.getStringExtra("id").toString();
        System.out.println("----------------++++++-----------"+id);
        if(status.equals("conner")){
            btn_choose.setText("已关注");
            flag = 1;
        }else{
            btn_choose.setText("关 注");
            flag = 0;
        }

        conner = (TextView) top.findViewById(R.id.visitor_conner);
        fans = (TextView) top.findViewById(R.id.visitor_fans);
        nickname = (TextView) top.findViewById(R.id.visitor_name);
        head_picture = top.findViewById(R.id.visitor_head);
        level = (TextView) top.findViewById(R.id.visitor_lv);
        personal_title = (TextView) top.findViewById(R.id.visitor_titl);
//            personal_title.setText("aaa");
        btn_choose.setOnClickListener(new choose_btn());

        init(id);
        imagesList = new ArrayList<>();


        initData(id);

        Map<String, Object> map = new HashMap<>();
        map.put("head_picture", "1");
        map.put("nickname", "1");
        map.put("time", "1");
        map.put("tagname", "1");
        myinformation = new ArrayList<>();
        myinformation.add(map);
        List<Image> list = new ArrayList<>();
        list.add(new Image("http://120.79.205.71/picture/pic1.jpg", 250, 250));
        imagesList.add(list);
        nvAdapter = new NVAdapter(Personal_visitor.this,imagesList, myinformation);
        listView.setAdapter(nvAdapter);

        listView.addHeaderView(top);
    }

    public class choose_btn implements View.OnClickListener{

        @Override
        public void onClick(View view) {

            if(1==flag){
                btn_choose.setText("已关注");
                flag = 0;
            }else{
                btn_choose.setText("关 注");
                flag = 1;
            }

        }
    }
    public void init(final String username){
        new Thread(new Runnable() {

            @Override
            public void run() {
//                System.out.println(username);
                String webContent = WebAccessTools.Get_Personal_Infor(username);
                System.out.println("web:" + webContent);
                personal_respinse = (Get_response) JsonUtil.jsonToObject(webContent,
                        Get_response.class);
                handler.sendEmptyMessage(0x123);
            }
        }).start();
    }
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (personal_respinse != null && personal_respinse.getMessage().equals("成功")) {
                Map<String, Object> map = new HashMap<String, Object>();
                try {
                    map = objectToMap(personal_respinse.getData());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                String conners="";
                if (personal_respinse.getData().get("conner_number")==null){
                    conners= "0";
                }else {
                    conners=personal_respinse.getData().get("conner_number").toString();
                }
                conner.setText(conners);

                String fan="";
                if(personal_respinse.getData().get("fans_number")==null){
                    fan="0";
                }else {
                    fan=personal_respinse.getData().get("fans_number").toString();
                }
                fans.setText(fan);

                Gson gson = new Gson();
                Map<String, Object> mapp = new HashMap<String, Object>();
                System.out.println("lllllllllllllll"+personal_respinse.getData().get("Information"));
                mapp = gson.fromJson(personal_respinse.getData().get("Information").toString(), map.getClass());
                nickname.setText(mapp.get("nickname").toString());
                level.setText("Lv"+mapp.get("levl").toString().substring(0,1));
                RequestOptions options = new RequestOptions().placeholder(R.mipmap.login_pic);
                Glide.with(Personal_visitor.this).asBitmap().load("http://120.79.205.71/picture/"+mapp.get("head_picture").toString()).apply(options).into(head_picture);
                if(level.getText().equals("Lv1")){
                    personal_title.setText("小萌新");
                }else{
                    personal_title.setText("小贱人");
                }
            }
        };
    };

    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = obj.getClass();
        System.out.println(clazz);
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            map.put(fieldName, value);
        }
        return map;
    }

    private void initData(final String username) {
        new Thread(new Runnable() {

            @Override
            public void run() {
//                System.out.println(username);
                String webContent = WebAccessTools.Get_Personal_Move(username);
                System.out.println("MOVEMENT:" + webContent);
                Movement_response = (Get_response_List) JsonUtil.jsonToObject(webContent,
                        Get_response_List.class);
                hand.sendEmptyMessage(0x123);
            }
        }).start();
    }

    @SuppressLint("HandlerLeak")
    private Handler hand = new Handler() {
        public void handleMessage(android.os.Message msg) {

            myinformation = new ArrayList<>();
            if (Movement_response != null && Movement_response.getMessage().equals("成功")) {
                List<Map<String, Object>> temp = Movement_response.getData();
                myinformation = new ArrayList<>();
                imagesList=new ArrayList<>();
                for (int i = 0; i < temp.size(); i++) {
                    //     String str = temp.get(i).get("tagname").toString().substring(1,temp.get(0).get("tagname").toString().length()-1);
                    String picture = temp.get(i).get("mypicture").toString().substring(1, temp.get(i).get("mypicture").toString().length() - 1);
                    picture = picture.replace(" ", "");
                    String[] arr = picture.split(",");
                    List<String> itemlist = Arrays.asList(arr);
                    ArrayList<Image> singleList = new ArrayList<>();
                    for (int j = 0; j < itemlist.size(); j++) {
                        singleList.add(new Image("http://120.79.205.71/picture/" + itemlist.get(j), 500, 500));
                    }
                    System.out.println("singls:" + singleList.size());
                    Map<String, Object> map = new HashMap<>();
                    map.put("nickname", Movement_response.getData().get(i).get("nickname").toString());
                    map.put("time", Movement_response.getData().get(i).get("time").toString());
                    map.put("head_picture", Movement_response.getData().get(i).get("head_picture").toString());
                    map.put("tagname", Movement_response.getData().get(i).get("tagname").toString());
                    myinformation.add(map);
                    imagesList.add(singleList);

                }
                System.out.println("+++++++++++++++++++:"+imagesList.size());
                System.out.println("------------------------:"+myinformation.size());

                nvAdapter.update(Personal_visitor.this,imagesList, myinformation);
            }
        }

        ;
    };

}


